﻿using System;
using System.Linq;
using static OnlineSupermarket.OnlineSupermarket;

namespace OnlineSupermarket
{
    public static class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("加载中……");
            PrepareSupermarkets(
                out OnlineSupermarket supermarket1,
                out OnlineSupermarket supermarket2);
            var cart = GetACart();
            Console.WriteLine("初始化完毕");
            Console.WriteLine("===================");

            for (; ; )
            {
                Console.WriteLine("接下来要做点什么呢？请输入序号：");
                Console.WriteLine("1. 逛超市");
                Console.WriteLine("2. 结账并退出");

                switch (Console.ReadLine())
                {
                    case "1":
                        var supermarket = ChooseSupermarket(supermarket1, supermarket2);
                        if (supermarket != null)
                            ShopIn(supermarket, cart);
                        Console.Clear();
                        break;
                    case "2":
                        if (cart.Count == 0)
                        {
                            Console.WriteLine("啊这，啥都没买！");
                            return;
                        }
                        var recipe = cart.CheckOutIn(supermarket1);
                        if (recipe == null)
                        {
                            Console.WriteLine("啊这，你买了什么奇奇怪怪的东西！");
                            return;
                        }
                        Console.WriteLine("=========收据=========");
                        foreach (var item in recipe.GoodsInfoAndWeights)
                            Console.WriteLine($"{item.Key.Name} {item.Value}×{item.Key.Price}￥");
                        Console.WriteLine($"计（不含运费和包装费）：{recipe.TotalPrice}");
                        Console.WriteLine($"运费和包装费：{recipe.FreightAndPackingFee}");
                        Console.WriteLine("======================");
                        return;
                    default:
                        Console.WriteLine("啥？再输入一次叭：");
                        break;
                }
            }
        }

        #region loading
        private static void PrepareSupermarkets(
            out OnlineSupermarket supermarket1,
            out OnlineSupermarket supermarket2)
        {
            supermarket1 = new OnlineSupermarket(
                    new GoodsInfo("苹果", 3.5m),
                    new GoodsInfo("桃子", 5m),
                    new GoodsInfo("荔枝", 5.5m),
                    new GoodsInfo("香蕉", 4m),
                    new GoodsInfo("橘子", 3m),
                    new GoodsInfo("桔子", 4.5m),
                    new GoodsInfo("梨子", 3m),
                    new GoodsInfo("水蜜桃", 5.3m));
            supermarket2 = new OnlineSupermarket(
                    new GoodsInfo("？？？", 3m),
                    new GoodsInfo("？？？？", 4m),
                    new GoodsInfo("？？？？？", 5m));
            Console.WriteLine("超市准备完毕");
        }

        private static Cart GetACart()
        {
            var result = new Cart();
            Console.WriteLine("购物车准备完毕");
            return result;
        }
        #endregion

        #region shopping
        private static OnlineSupermarket? ChooseSupermarket(
            OnlineSupermarket supermarket1, OnlineSupermarket supermarket2)
        {
            Console.Clear();
            for (; ; )
            {
                Console.WriteLine("请选择一家超市吧！你想去：");
                Console.WriteLine("1. 一家只卖水果的超市");
                Console.WriteLine("2. 另一家奇奇怪怪的超市");
                Console.WriteLine("b. 返回上一级");

                switch (Console.ReadLine())
                {
                    case "1":
                        return supermarket1;
                    case "2":
                        return supermarket2;
                    case "b":
                        return null;
                    default:
                        Console.WriteLine("啥？再输入一次叭：");
                        break;
                }
            }
        }
        private static void ShopIn(OnlineSupermarket supermarket, Cart cart)
        {
            Console.Clear();
            Console.Write("进入超市啦！");
            for (; ; )
            {
                Console.WriteLine("接下来：");
                var goods = supermarket.ToArray();
                for (int i = 1; i <= goods.Length; i++)
                {
                    var current = goods[i - 1];
                    Console.WriteLine($"{i}. 购买 {current.Name} （￥{current.Price}）");
                }
                Console.WriteLine("e. 离开超市");

                var input = Console.ReadLine();
                if (input == "e")
                    return;
                if (int.TryParse(input, out int choice) &&
                    choice <= goods.Length && choice > 0)
                {
                    var chosenGoods = goods[choice - 1];
                    cart.Add(chosenGoods);
                    Console.Write($"已将 {chosenGoods.Name} 加入购物车！");
                }
                else
                    Console.Write("啥？再输入一次叭！");
            }
        }
        #endregion
    }
}